import {
  successResponse,
  errorResponse,
  validationErrorResponse,
} from "server/utils/response";
import { Projects } from "server/models/Project";

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event);

    if (!id) {
      return validationErrorResponse("项目ID不能为空");
    }

    const projectId = parseInt(id);
    if (isNaN(projectId)) {
      return validationErrorResponse("项目ID格式不正确");
    }

    // 使用数据库查询获取项目详情
    const project = await Projects.getProjectById(projectId);

    if (!project) {
      return errorResponse("项目不存在");
    }

    // 返回项目数据，确保字段名称与前端期望一致
    return successResponse(
      {
        id: project.id,
        title: project.title,
        description: project.description,
        content: project.content,
        category: project.category,
        technologies: project.technologies,
        status: project.status,
        is_featured: project.is_featured,
        cover_image_id: project.cover_image_id,
        cover_image_url: project.cover_image_url,
        demo_url: project.demo_url,
        github_url: project.github_url,
        role: project.role,
      },
      "获取项目详情成功"
    );
  } catch (error) {
    console.error("获取项目详情失败:", error);
    return errorResponse(
      "获取项目详情失败",
      error instanceof Error ? error.message : "未知错误"
    );
  }
});
